{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "OSError",
     "evalue": "[WinError 10049] 在其上下文中，该请求的地址无效。",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mOSError\u001b[0m                                   Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-91487275571a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     11\u001b[0m \u001b[0mtcpS\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msocket\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mAF_INET\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mSOCK_STREAM\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 创建socket对象\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     12\u001b[0m \u001b[0mtcpS\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msetsockopt\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mSOL_SOCKET\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mSO_REUSEADDR\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m#加入socket配置，重用ip和端口\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 13\u001b[1;33m \u001b[0mtcpS\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbind\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mADDR\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 绑定ip端口号\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     14\u001b[0m \u001b[0mtcpS\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlisten\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mSIZE\u001b[0m\u001b[1;33m)\u001b[0m  \u001b[1;31m# 设置最大链接数\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     15\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mOSError\u001b[0m: [WinError 10049] 在其上下文中，该请求的地址无效。"
     ]
    }
   ],
   "source": [
    "from socket import *\n",
    "import time\n",
    "\n",
    "COD = 'utf-8'\n",
    "HOST = '10.32.3.200'\n",
    "PORT = 9013\n",
    "BUFSIZE = 1024\n",
    "ADDR = (HOST,PORT)\n",
    "SIZE = 10\n",
    "\n",
    "tcpS = socket(AF_INET, SOCK_STREAM) # 创建socket对象\n",
    "tcpS.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) #加入socket配置，重用ip和端口\n",
    "tcpS.bind(ADDR) # 绑定ip端口号\n",
    "tcpS.listen(SIZE)  # 设置最大链接数\n",
    "\n",
    "while True:\n",
    "    print('启动服务，监听客服端连接')\n",
    "    conn,addr = tcpS.accept()\n",
    "    print('连接的客服端：',addr)\n",
    "    while True:\n",
    "        try:\n",
    "             data = conn.recv(BUFSIZE)# 读取已链接客户的发送的消息\n",
    "        except Exception:\n",
    "            print('断开的客服端：',addr)\n",
    "            break\n",
    "        print('客服端发送的内容：',data.decode(COD))\n",
    "        if not data:\n",
    "            break\n",
    "            \n",
    "        msg = time.strftime(\"%Y-%m-%d %X\") #获取结构化事件戳\n",
    "        msg1 = '服务端 ：'+'[%s]:%s' % (msg, data.decode(COD))\n",
    "        conn.send(msg1.encode(COD)) #发送消息给已链接客户端\n",
    "    conn.close()\n",
    "    \n",
    "tcpS.close()\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
